package com.huanglj.taobaounion.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.taobaounion.R;
import com.huanglj.taobaounion.model.domain.SelectedPage;
import com.huanglj.taobaounion.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedPageLeftAdapter extends RecyclerView.Adapter<SelectedPageLeftAdapter.InnerHolder> {
    private static final String TAG = "SelectedPageLeftAdapter";

    private List<SelectedPage.DataBean> mData = new ArrayList<>();
    private OnItemClickListener mListener;
    private int mCurrentItem = 0;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_left_list, parent, false);
        return new InnerHolder(view);
    }

    @Override

    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position),position);
    }

    @Override
    public int getItemCount() {
        LogUtil.d(TAG,"data size--->" + mData.size());
        return mData.size();
    }

    public void setData(List<SelectedPage.DataBean> result) {
        mData.clear();
        mData.addAll(result);
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_left_title)
        TextView leftTitle;
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setData(SelectedPage.DataBean dataBean, int position) {
            leftTitle.setText(dataBean.getFavorites_title());
            if (mCurrentItem == position) {
                itemView.setBackground(itemView.getResources().getDrawable(R.color.selectDefault,null));
            }else {
                itemView.setBackground(itemView.getResources().getDrawable(R.color.white,null));
            }
            itemView.setOnClickListener(v -> {
                if (mCurrentItem != position) {
                    mCurrentItem = position;
                    /**
                     * 就他妈因为一个这个 notifyDataSetChanged()给折腾了2小时，妈的智障，留下纪念一下
                     */

                    notifyDataSetChanged();
                }
                if (mListener!=null) {
                    mListener.onItemClick(dataBean);
                }
            });
        }
    }
    public interface OnItemClickListener{
        void onItemClick(SelectedPage.DataBean item);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener = listener;
    }
}
